package com.shzq.service.impl;


import com.shzq.constant.GradeEnum;
import com.shzq.dao.CountMapper;
import com.shzq.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;


@Service
@Transactional
public class ReportServiceImpl implements ReportService {

    @Autowired
    private CountMapper countMapper;
    //统计地震总数
    public List<Integer>  addAll(List<Integer> data){
        int k=0;
        List<Integer> total = new ArrayList<>();
        for(int i=0;i<data.size();i++){
            k=data.get(i)+k;
        }
        total.add(k);
        return total;
    }

    @Override
    public Map<String,List> countByYearAndDepth(String year) {
        List<Integer> data = new LinkedList<>();
        List<String> describe = new LinkedList<>();
        Map<String,List> map = new HashMap<>();
        GradeEnum[] values = GradeEnum.values();
        Arrays.stream(values).forEach((grade)->{
            Integer[] depth = grade.getDepth();
            HashMap<String, Object> parMap = new HashMap<>();
            parMap.put("year",year);
            parMap.put("end",depth[1]);
            parMap.put("begin",depth[0]);
            Integer count = countMapper.countByTimeQuantumWithDepth(parMap);
            data.add(count);
            describe.add(depthScope2String(grade));
        });

        map.put("total",addAll(data));
        map.put("data",data);
        map.put("describe",describe);
        return map;
    }
    @Override
    public Map<String,List> countByQuarterAndDepth(String m_begin,String m_end) {
        List<Integer> data = new LinkedList<>();
        List<String> describe = new LinkedList<>();
        Map<String,List> map = new HashMap<>();
        GradeEnum[] values = GradeEnum.values();
        Arrays.stream(values).forEach((grade)->{
            Integer[] depth = grade.getDepth();
            HashMap<String, Object> parMap = new HashMap<>();
            parMap.put("month_begin",m_begin);
            parMap.put("month_end",m_end);
            parMap.put("end",depth[1]);
            parMap.put("begin",depth[0]);
            Integer count = countMapper.countByQuarterWithDepth(parMap);
            data.add(count);
            describe.add(depthScope2String(grade));
        });

        map.put("total",addAll(data));
        map.put("data",data);
        map.put("describe",describe);
        return map;
    }

    private String depthScope2String(GradeEnum grade){
        StringBuilder sb = new StringBuilder();
        Integer[] depth = grade.getDepth();
        //a-b  >b <a
        if(depth[0]!=null&&depth[1]!=null){
            sb.append(depth[0]).append("-").append(depth[1]);
        }
        else if(depth[0]==null){
            sb.append(">").append(depth[1]);
        }
        else sb.append(">").append(depth[0]);
        return sb.toString();
    }

    @Override
    public Map<String, List> countByYearAndStrength(String year) {
        List<Integer> data = new LinkedList<>();
        List<String> describe = new LinkedList<>();
        Map<String, List> map = new HashMap<>();
        GradeEnum[] values = GradeEnum.values();
        Arrays.stream(values).forEach((grade) -> {
            Integer[] strength = grade.getStrength();
            HashMap<String, Object> parMap = new HashMap<>();
            parMap.put("year", year);
            parMap.put("end", strength[1]);
            parMap.put("begin", strength[0]);
            Integer count = countMapper.countByTimeQuantumWithStrength(parMap);

            data.add(count);
            describe.add(strengthScope2String(grade));
        });
        map.put("total",addAll(data));
        map.put("data", data);
        map.put("describe", describe);
        return map;
    }
    @Override
    public Map<String,List> countByQuarterAndStrength(String m_begin,String m_end) {
        List<Integer> data = new LinkedList<>();
        List<String> describe = new LinkedList<>();
        Map<String,List> map = new HashMap<>();
        GradeEnum[] values = GradeEnum.values();
        Arrays.stream(values).forEach((grade)->{
            Integer[] strength = grade.getStrength();
            HashMap<String, Object> parMap = new HashMap<>();
            parMap.put("month_begin",m_begin);
            parMap.put("month_end",m_end);
            parMap.put("end",strength[1]);
            parMap.put("begin",strength[0]);
            Integer count = countMapper.countByQuarterWithStrength(parMap);
            data.add(count);
            describe.add(strengthScope2String(grade));
        });

        map.put("total",addAll(data));
        map.put("data",data);
        map.put("describe",describe);
        return map;
    }

    private String strengthScope2String(GradeEnum grade) {
        StringBuilder sb = new StringBuilder();
        Integer[] strength = grade.getStrength();
        //a-b  >b <a
        if (strength[0] != null && strength[1] != null) {
            sb.append(strength[0]).append("-").append(strength[1]);
        } else if (strength[0] == null) {
            sb.append("<").append(strength[1]);
        } else sb.append(">").append(strength[0]);
        return sb.toString();
    }
}
